package com.gf.help.dao.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import com.gf.base.dao.impl.BaseDaoImpl;
import com.gf.help.dao.QuestionDao;
import com.gf.help.mapper.QuestionMapper;
import com.gf.help.model.Question;

@Repository("questionDao")
public class QuestionDaoImpl extends BaseDaoImpl implements QuestionDao {
	@Autowired
	private JdbcTemplate jdbcTemplate;
	/**
	 * select * from question order by publishTime desc limit ?,?
	 */
	@Value("${questionDao.queryQuestionsSql}")
	private String queryQuestionsSql;

	@Value("${questionDao.queryCountSql}")
	private String queryCountSql;

	@Value("${questionDao.queryByIdSql}")
	private String queryByIdSql;

	@Value("${questionDao.updateSql}")
	private String updateSql;

	@Value("${questionDao.deleteSql}")
	private String deleteSql;

	@Value("${questionDao.insertSql}")
	private String insertSql;

	@Value("${questionDao.queryQuestionsByAskerIdSql}")
	private String queryQuestionsByAskerIdSql;

	@Value("${questionDao.queryQuestionsByApplIerIdSql}")
	private String queryQuestionsByApplIerIdSql;

	@Value("${questionDao.updateReadCountSql}")
	private String updateReadCountSql;

	@Value("${questionDao.updateReplyCountSql}")
	private String updateReplyCountSql;

	@Value("${questionDao.queryCountQuestionsByApplIerIdSql}")
	private String queryCountQuestionsByApplIerIdSql;

	@Value("${questionDao.queryCountQuestionsByAskIdSql}")
	private String queryCountQuestionsByAskIdSql;

	@Override
	public List<Question> queryQuestions(int currentPage, int pageSize) {
		List<Question> questions = jdbcTemplate.query(queryQuestionsSql,
				QuestionMapper.getMapper(), (currentPage - 1) * pageSize,
				pageSize);
		return questions;
	}

	@Override
	public int queryCount() {

		return jdbcTemplate.queryForInt(queryCountSql);
	}

	@Override
	public Question queryById(long id) {
		List<Question> questions = jdbcTemplate.query(queryByIdSql,
				QuestionMapper.getMapper(), id);
		if (questions != null && !questions.isEmpty()) {
			return questions.get(0);
		}
		return null;
	}

	@Override
	public boolean update(Question question) {
		int i = jdbcTemplate.update(updateSql, question.getAskerName(),
				question.getContent(), question.getTitle(),
				question.getPublishTime(), question.getId());
		if (i != 0) {
			return true;
		}
		return false;
	}

	@Override
	public boolean delete(long id) {
		int i = jdbcTemplate.update(deleteSql, id);
		if (i != 0) {
			return true;
		}
		return false;
	}

	@Override
	public boolean insert(Question question) {
		int i = jdbcTemplate.update(insertSql, question.getContent(),
				question.getTitle(), question.getAskerId(),
				question.getAskerName(), question.getAskerPortrait());
		if (i != 0) {
			return true;
		}
		return false;
	}

	@Override
	public List<Question> queryQuestionsByAskerId(long askerId,
			int currentPage, int pageSize) {
		List<Question> questions = jdbcTemplate.query(
				queryQuestionsByAskerIdSql, QuestionMapper.getMapper(),
				askerId, (currentPage - 1) * pageSize, pageSize);
		return questions;
	}

	@Override
	public List<Question> queryQuestionsByApplIerId(long applierId,
			int currentPage, int pageSize) {
		List<Question> questions = jdbcTemplate.query(
				queryQuestionsByApplIerIdSql, QuestionMapper.getMapper(),
				applierId, (currentPage - 1) * pageSize, pageSize);
		return questions;
	}

	@Override
	public boolean updateReadCount(long id, int count) {
		int i = jdbcTemplate.update(updateReadCountSql, count, id);
		if (i != 0) {
			return true;
		}
		return false;
	}

	@Override
	public boolean updateReplyCount(long id, int replyCount) {
		int i = jdbcTemplate.update(updateReplyCountSql, replyCount, id);
		if (i != 0) {
			return true;
		}
		return false;
	}

	@Override
	public int queryCountQuestionsByApplIerId(long applierId) {
		return jdbcTemplate.queryForInt(queryCountQuestionsByApplIerIdSql,
				applierId);
	}

	@Override
	public int queryCountQuestionsByAskId(long askId) {
		return jdbcTemplate.queryForInt(queryCountQuestionsByAskIdSql, askId);
	}

}
